#!/bin/env/python3
# 自定义过滤器
from functools import wraps

from flask import session, current_app, g


def hot_news_filter(index):
    index_dict = {1: "first", 2: "second", 3: "third"}
    return index_dict.get(index, "")


# 定义登录装饰器，封装用户的登录数据
def user_login_data(view_func):
    @wraps(view_func)
    def wrapper(*args, **kwargs):
        user_id = session.get("user_id")
        user = None
        try:
            from info.models import User
            user = User.query.get(user_id)
        except Exception as e:
            current_app.logger.error(e)
        g.user = user
        # user对象封装入g
        return view_func(*args, **kwargs)

    return wrapper
